package com.youkeyi.ddy.cloud.basic.crm.recipient.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.recipient.entity.SysVaccineRecipient;
import com.youkeyi.ddy.cloud.basic.crm.recipient.service.SysVaccineRecipientService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.CrmVaccineRecipientCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.dto.CrmVaccineRecipientListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.dto.CrmVaccineRecipientListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 疫苗接种者表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-19
 */
@Service
public class SysVaccineRecipientController implements CrmVaccineRecipientCloudService {
    
    @Autowired
    private SysVaccineRecipientService sysVaccineRecipientService;

    @Override
    public CrmVaccineRecipientListCloudDto query(CrmVaccineRecipientQueryCloudParam param) {
        CrmVaccineRecipientListCloudDto dto = new CrmVaccineRecipientListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getVaccineRecipientPrimaryKeys())) {
            param.getVaccineRecipientPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerKeys())) {
            param.getCustomerKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysVaccineRecipient> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getVaccineRecipientPrimaryKeys()), SysVaccineRecipient::getPrimaryKey, param.getVaccineRecipientPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysVaccineRecipient::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerKeys()), SysVaccineRecipient::getCustomerKey, param.getCustomerKeys())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysVaccineRecipient::getName, param.getNameFuzzy())
                .like(StringUtils.isNotBlank(param.getIdCardFuzzy()), SysVaccineRecipient::getIdCard, param.getIdCardFuzzy())
                .eq(StringUtils.isNotBlank(param.getIdCard()), SysVaccineRecipient::getIdCard, param.getIdCard())
                .ne(SysVaccineRecipient::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysVaccineRecipient> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysVaccineRecipient> vaccineRecipientList = sysVaccineRecipientService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(vaccineRecipientList.getRecords())) {
            dto.setTotal(vaccineRecipientList.getTotal());

            List<CrmVaccineRecipientListItemCloudDto> items = new ArrayList<>();
            vaccineRecipientList.getRecords().forEach(vaccineRecipient -> {
                CrmVaccineRecipientListItemCloudDto item = new CrmVaccineRecipientListItemCloudDto();
                item.setVaccineRecipientPrimaryKey(vaccineRecipient.getPrimaryKey());
                item.setCorpKey(vaccineRecipient.getCorpKey());
                item.setCustomerKey(vaccineRecipient.getCustomerKey());
                item.setCustomerName(vaccineRecipient.getCustomerName());
                item.setName(vaccineRecipient.getName());
                item.setSex(vaccineRecipient.getSex());
                item.setBirthday(vaccineRecipient.getBirthday() != null ? vaccineRecipient.getBirthday().getTime() : null);
                item.setIdCard(vaccineRecipient.getIdCard());
                item.setRelationship(vaccineRecipient.getRelationship());
                item.setRemark(vaccineRecipient.getRemark());
                item.setIsDeleted(vaccineRecipient.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(vaccineRecipient.getCreateTime() != null ? vaccineRecipient.getCreateTime().getTime() : null);
                item.setCreateUserKey(vaccineRecipient.getCreateUserKey());
                item.setCreateUserName(vaccineRecipient.getCreateUserName());
                item.setUpdateTime(vaccineRecipient.getUpdateTime() != null ? vaccineRecipient.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(vaccineRecipient.getUpdateUserKey());
                item.setUpdateUserName(vaccineRecipient.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmVaccineRecipientCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysVaccineRecipient vaccineRecipient = new SysVaccineRecipient();
        vaccineRecipient.setPrimaryKey(SimpleUUidUtils.getUuid());
        vaccineRecipient.setCorpKey(param.getCorpKey());
        vaccineRecipient.setCustomerKey(param.getCustomerKey());
        vaccineRecipient.setCustomerName(param.getCustomerName());
        vaccineRecipient.setName(param.getName());
        vaccineRecipient.setSex(param.getSex());
        vaccineRecipient.setBirthday(param.getBirthday() != null ? new Date(param.getBirthday()) : null);
        vaccineRecipient.setIdCard(param.getIdCard());
        vaccineRecipient.setRelationship(param.getRelationship());
        vaccineRecipient.setRemark(param.getRemark());
        vaccineRecipient.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        vaccineRecipient.setCreateUserKey(param.getCreateUserKey());
        vaccineRecipient.setCreateUserName(param.getCreateUserName());
        vaccineRecipient.setCreateTime(new Date());
        boolean save = sysVaccineRecipientService.save(vaccineRecipient);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(vaccineRecipient.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmVaccineRecipientUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getVaccineRecipientPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysVaccineRecipient> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysVaccineRecipient::getPrimaryKey, param.getVaccineRecipientPrimaryKey());
        SysVaccineRecipient vaccineRecipient = new SysVaccineRecipient();
        vaccineRecipient.setPrimaryKey(param.getVaccineRecipientPrimaryKey());
        vaccineRecipient.setName(param.getName());
        vaccineRecipient.setCustomerKey(param.getCustomerKey());
        vaccineRecipient.setCustomerName(param.getCustomerName());
        vaccineRecipient.setName(param.getName());
        vaccineRecipient.setSex(param.getSex());
        vaccineRecipient.setBirthday(param.getBirthday() != null ? new Date(param.getBirthday()) : null);
        vaccineRecipient.setIdCard(param.getIdCard());
        vaccineRecipient.setRelationship(param.getRelationship());
        vaccineRecipient.setRemark(param.getRemark());
        vaccineRecipient.setUpdateUserKey(param.getUpdateUserKey());
        vaccineRecipient.setUpdateUserName(param.getUpdateUserName());
        vaccineRecipient.setUpdateTime(new Date());
        boolean save = sysVaccineRecipientService.update(vaccineRecipient, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmVaccineRecipientDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getVaccineRecipientPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysVaccineRecipient> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysVaccineRecipient::getPrimaryKey, param.getVaccineRecipientPrimaryKeys());

        SysVaccineRecipient vaccineRecipient = new SysVaccineRecipient();
        vaccineRecipient.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        vaccineRecipient.setUpdateUserKey(param.getUpdateUserKey());
        vaccineRecipient.setUpdateUserName(param.getUpdateUserName());
        vaccineRecipient.setUpdateTime(new Date());
        boolean save = sysVaccineRecipientService.update(vaccineRecipient, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
